/* GUI CONVERTED from rsporct.p (converter v1.76) Wed May 22 11:23:05 2002 */
/* rsporct.p - Tolerance Checking for PO receipts in Supplier Schedule module */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.4.1.2 $                                                       */
/*V8:ConvertMode=Maintenance                                                  */
/* REVISION: 7.3    LAST MODIFIED: 09/18/95           BY: vrn *G0X3*          */
/* REVISION: 8.5    LAST MODIFIED: 01/17/95           BY: taf *J053*          */
/* REVISION: 8.5    LAST MODIFIED: 03/08/96           BY: vrn *G1NV*          */
/* REVISION: 8.6    LAST MODIFIED: 05/20/98           BY: *K1Q4* Alfred Tan   */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00           BY: *N0KP* myb          */
/* $Revision: 1.4.1.2 $    BY: Jean Miller       DATE: 05/20/02  ECO: *P05V*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* FUNCTION : Checks the see whether the quantity that has been received
          is within the quantity and cost tolerance specified. Can do
          this for both discrete and scheduled purchase orders    */

/* NOTE : Code has been transplanted from rsporc.p. */

{mfdeclre.i}

define input  parameter qty_rcvd  like pod_qty_rcvd no-undo.
define input  parameter po_recid as recid no-undo.
define input  parameter pod_recid as recid no-undo.
define input  parameter eff_date  as date no-undo.
define input  parameter tol_pct   like poc_tol_pct no-undo.
define input  parameter tol_cst   like poc_tol_cst no-undo.
define input  parameter base_amt  like pod_pur_cost no-undo.
define input  parameter warn_only as logical no-undo.
define input  parameter disp_ord_line as logical no-undo.
define output parameter undo_loop like mfc_logical no-undo.

define shared variable rndmthd like rnd_rnd_mthd.
define variable divisor     like sod_qty_ord no-undo.
define variable overage_qty as   decimal no-undo.
define variable qty_open    like pod_qty_ord no-undo.
define variable msg_level   as integer no-undo.

undo_loop = true.

find po_mstr where recid(po_mstr) = po_recid no-lock.
find pod_det where recid(pod_det) = pod_recid no-lock.

find first gl_ctrl no-lock.

assign
   qty_open = 0
   divisor =  0
   undo_loop = false.

mainblock:
do on endkey undo mainblock,leave mainblock:

   if pod_sched then do:

      /* If with the new receipt we would exceed the Max Order qty specified
         for the Scheduled PO then warn . The pause after the message is
         there because of a "hide message no-pause" before overshipment
         message below. In case both these conditions (overshipment &
         over Max Order qty) were to be true at the same time, the following
         msg will flash by. On the other hand, removing the "hide message
         no-pause" would cause the two messages that appear in case of
         overshipment not to appear all at once. */
      if  pod_cum_qty[3] > 0 and pod_cum_qty[1] +
         (qty_rcvd / pod_um_conv) >= pod_cum_qty[3]
      then do:
         hide message no-pause.
         /* CUM RCVD QTY GREATER OR EQUAL */
         /* MAX ORDER QTY FOR ORDER SELECTED*/
         {pxmsg.i &MSGNUM=8232 &ERRORLEVEL=2}
         if disp_ord_line then do:
            /* Order #, Line # */
            {pxmsg.i &MSGNUM=8310 &ERRORLEVEL=1
                     &MSGARG1=pod_nbr
                     &MSGARG2=pod_line}
            /* Order <> Line <> */
         end.
         if not batchrun then pause.
      end.

      find last schd_det
         where schd_type = 4
         and schd_nbr = pod_nbr
         and schd_line = pod_line
         and schd_rlse_id = pod_curr_rlse_id[1]
         and schd_date <= eff_date
      no-lock no-error.

      if available schd_det then do:
         assign
            qty_open = schd_cum_qty - pod_cum_qty[1]
            divisor = schd_discr_qty.
      end.

      else do:
         find sch_mstr where sch_type = 4
            and sch_nbr = pod_nbr
            and sch_line = pod_line
            and sch_rlse_id = pod_curr_rlse_id[1]
         no-lock no-error.
         if available sch_mstr then do:
            assign
               qty_open = sch_pcr_qty - pod_cum_qty[1]
               divisor = qty_open.
            if divisor < 0 then divisor = 0 - divisor.
         end.
      end.

      overage_qty = (qty_rcvd / pod_um_conv) - qty_open.

   end.

   /* if not pod_sched */
   else do:
      assign
         overage_qty =
            pod_qty_rcvd + (qty_rcvd / pod_um_conv) - pod_qty_ord
         divisor = pod_qty_ord.
      if divisor < 0 then divisor = 0 - divisor.
   end. /* else if not pod_sched */

   /*FIRST CHECK PERCENT OVERSHIP*/
   if divisor = 0 or (overage_qty * 100) / divisor > tol_pct
   then do:

      hide message no-pause.
      if pod_sched then do:
         /*OVERSHIP % EXCEEDS SCHEDULE AS OF <date>
         TOLERANCE: <pct> */
         {pxmsg.i &MSGNUM=8305 &ERRORLEVEL=2
                  &MSGARG1=eff_date
                  &MSGARG2=tol_pct}
       end.
      else do:
         msg_level = if warn_only then 2 else 4.
         /* Warning is enough for a schd order but not for
            discrete. Discrete PO receipts can't exceed tolerance */
         {pxmsg.i &MSGNUM=337 &ERRORLEVEL=msg_level &MSGARG1=tol_pct}
         undo_loop = true.
      end.

      if disp_ord_line then do:
         /* Order <> Line <> */
         /* Order #, Line # */
         {pxmsg.i &MSGNUM=8310 &ERRORLEVEL=1
                  &MSGARG1=pod_nbr
                  &MSGARG2=pod_line}
         /* The purpose of this "message" is stop processing
         from continuing and "catch" it here. This gives a chance
         to the user to F4 and discontinue the process */
      end.

      if not batchrun then pause.

   end.

   /*NEXT CHECK DOLLAR AMOUNT OVERSHIP*/

   if overage_qty * base_amt > tol_cst then do:

      hide message no-pause.
      if pod_sched then do:
         /* OVERSHIP COST EXCEEDS SCHEDULE AS OF <date> TOLERANCE: <amt> */
         {pxmsg.i &MSGNUM=8306 &ERRORLEVEL=2
                  &MSGARG1=eff_date
                  &MSGARG2=tol_cst}
      end.
      else do:
         /* Warning is enough for a schd order but not for
         discrete. Discrete PO receipt amts can't exceed tolerance */
         msg_level = if warn_only then 2 else 4.
         {pxmsg.i &MSGNUM=338 &ERRORLEVEL=msg_level &MSGARG1=tol_cst}
         undo_loop = true.
      end.
      if disp_ord_line then do:
         /* Order #, Line # */
         {pxmsg.i &MSGNUM=8310 &ERRORLEVEL=1
                  &MSGARG1=pod_nbr
                  &MSGARG2=pod_line}
      end.

      if not batchrun then pause.

   end.

end. /* mainblock */

/* If user has pressed F4 at one of the above "pauses" */
if keyfunction(lastkey) = "end-error" then undo_loop = true.
